<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

define( "JIEQI_MODULE_NAME", "system" );
require_once( "../global.php" );
include_once( JIEQI_ROOT_PATH."/class/power.php" );
$power_handler =& jieqipowerhandler::getinstance( "JieqiPowerHandler" );
$power_handler->getsavedvars( "system" );
jieqi_checkpower( $jieqiPower['system']['adminmessage'], $jieqiUsersStatus, $jieqiUsersGroup, false, true );
jieqi_loadlang( "message", JIEQI_MODULE_NAME );
if ( !isset( $_REQUEST['action'] ) )
{
		$GLOBALS['_REQUEST']['action'] = "message";
}
switch ( $_REQUEST['action'] )
{
case "newmessage" :
		$GLOBALS['_REQUEST']['receiver'] = trim( $_REQUEST['receiver'] );
		$GLOBALS['_REQUEST']['title'] = trim( $_REQUEST['title'] );
		$errtext = "";
		if ( strlen( $_REQUEST['receiver'] ) == 0 )
		{
				$errtext .= $jieqiLang['system']['message_need_receiver']."<br />";
		}
		if ( strlen( $_REQUEST['title'] ) == 0 )
		{
				$errtext .= $jieqiLang['system']['message_need_title']."<br />";
		}
		if ( empty( $errtext ) )
		{
				include_once( JIEQI_ROOT_PATH."/class/users.php" );
				$users_handler = jieqiusershandler::getinstance( "JieqiUsersHandler" );
				$touser = $users_handler->getbyname( $_REQUEST['receiver'], 3 );
				if ( !$touser )
				{
						jieqi_printfail( $jieqiLang['system']['message_no_receiver'] );
				}
				include_once( JIEQI_ROOT_PATH."/class/message.php" );
				$message_handler =& jieqimessagehandler::getinstance( "JieqiMessageHandler" );
				$newMessage = $message_handler->create( );
				$newMessage->setvar( "siteid", JIEQI_SITE_ID );
				$newMessage->setvar( "postdate", JIEQI_NOW_TIME );
				$newMessage->setvar( "fromid", 0 );
				$newMessage->setvar( "fromname", $_SESSION['jieqiUserName'] );
				$newMessage->setvar( "toid", $touser->getvar( "uid", "n" ) );
				if ( 0 < strlen( $touser->getvar( "name", "n" ) ) )
				{
						$newMessage->setvar( "toname", $touser->getvar( "name", "n" ) );
				}
				else
				{
						$newMessage->setvar( "toname", $touser->getvar( "uname", "n" ) );
				}
				$newMessage->setvar( "title", $_REQUEST['title'] );
				$newMessage->setvar( "content", $_REQUEST['content'] );
				$newMessage->setvar( "messagetype", 0 );
				$newMessage->setvar( "isread", 0 );
				$newMessage->setvar( "fromdel", 0 );
				$newMessage->setvar( "todel", 0 );
				$newMessage->setvar( "enablebbcode", 1 );
				$newMessage->setvar( "enablehtml", 0 );
				$newMessage->setvar( "enablesmilies", 1 );
				$newMessage->setvar( "attachsig", 0 );
				$newMessage->setvar( "attachment", 0 );
				if ( !$message_handler->insert( $newMessage ) )
				{
						jieqi_printfail( $jieqiLang['system']['message_send_failure'] );
				}
				else
				{
						jieqi_jumppage( JIEQI_URL."/admin/message.php?box=inbox", LANG_DO_SUCCESS, $jieqiLang['system']['message_send_seccess'] );
				}
		}
		else
		{
				jieqi_printfail( $errtext );
		}
		break;
case "message" :
		include_once( JIEQI_ROOT_PATH."/admin/header.php" );
		$message = false;
		if ( !empty( $_REQUEST['reid'] ) && !empty( $_REQUEST['fwid'] ) )
		{
				include_once( JIEQI_ROOT_PATH."/class/message.php" );
				$message_handler = jieqimessagehandler::getinstance( "JieqiMessageHandler" );
				if ( !empty( $_REQUEST['reid'] ) )
				{
						$message = $message_handler->get( $_REQUEST['reid'] );
				}
				else if ( !empty( $_REQUEST['fwid'] ) )
				{
						$message = $message_handler->get( $_REQUEST['fwid'] );
				}
		}
		if ( is_object( $message ) )
		{
				$GLOBALS['_REQUEST']['receiver'] = $message->getvar( "fromname", "e" );
				$GLOBALS['_REQUEST']['title'] = $message->getvar( "title", "e" );
				if ( !empty( $_REQUEST['reid'] ) )
				{
						$GLOBALS['_REQUEST']['title'] = "Re:".$_REQUEST['title'];
						$GLOBALS['_REQUEST']['content'] = "";
				}
				else if ( !empty( $_REQUEST['fwid'] ) )
				{
						$GLOBALS['_REQUEST']['title'] = "Fw:".$_REQUEST['title'];
						$GLOBALS['_REQUEST']['content'] = $message->getvar( "content", "e" );
				}
		}
		if ( !isset( $_REQUEST['receiver'] ) )
		{
				$GLOBALS['_REQUEST']['receiver'] = "";
		}
		if ( !isset( $_REQUEST['title'] ) )
		{
				$GLOBALS['_REQUEST']['title'] = "";
		}
		if ( !isset( $_REQUEST['content'] ) )
		{
				$GLOBALS['_REQUEST']['content'] = "";
		}
		$jieqiTpl->assign( "url_newmessage", JIEQI_URL."/admin/newmessage.php" );
		$jieqiTpl->assign( "receiver", $_REQUEST['receiver'] );
		$jieqiTpl->assign( "title", $_REQUEST['title'] );
		$jieqiTpl->assign( "content", $_REQUEST['content'] );
		$jieqiTpl->assign( "action", "newmessage" );
		$jieqiTpl->setcaching( 0 );
		$jieqiTset['jieqi_contents_template'] = JIEQI_ROOT_PATH."/templates/admin/newmessage.html";
		include_once( JIEQI_ROOT_PATH."/admin/footer.php" );
}
?>
